package com.cxs.xiaomi.service.impl;

import com.cxs.xiaomi.dao.GoodsDao;
import com.cxs.xiaomi.dao.GoodsTypeDao;
import com.cxs.xiaomi.dao.impl.GoodsDaoImpl;
import com.cxs.xiaomi.dao.impl.GoodsTypeDaoImpl;
import com.cxs.xiaomi.domain.Goods;
import com.cxs.xiaomi.domain.GoodsType;
import com.cxs.xiaomi.service.GoodsService;
import com.cxs.xiaomi.utils.FileUploadUtil;
import com.cxs.xiaomi.utils.Page;
import com.cxs.xiaomi.utils.StringUtil;
import java.util.ArrayList;
import java.util.List;

/*
 * @Author:cxs
 * @Date:2021/9/14  22:15
 * @Version 1.0
 * @Motto:放下杂念,只为迎接明天更好的自己
 * */
public class GoodsServiceImpl implements GoodsService {

    private GoodsDao goodsDao = new GoodsDaoImpl();
    private GoodsTypeDao goodsTypeDao = new GoodsTypeDaoImpl();

    @Override
    public Page<Goods> findGoodByType(String pageNum, String pageSize, String preSql, List list) {
        if (StringUtil.empty(pageNum)) {
            pageNum = 1 + "";
        }
        if (StringUtil.empty(pageSize)) {
            pageSize = 8 + "";
        }
        // 总数
        Object count = goodsDao.selectCountsByConditon(preSql, list);
        Page page = new Page(Integer.parseInt(pageNum.trim()), Integer.parseInt(pageSize.trim()), Integer.parseInt(count.toString()));
        String condition = preSql + " limit ?,?";
        list.add(page.getStartIndex());
        list.add(page.getPageSize());
        List<Goods> goodsList = goodsDao.selectByCondition(condition, list);
        page.setList(goodsList);
        return page;
    }

    @Override
    public Goods getDetail(String id) {
        Goods goods = goodsDao.selectById(Integer.parseInt(id));
        if (goods != null) {
            GoodsType goodsType = goodsTypeDao.selectById(goods.getTypeid());
            goods.setGoodsType(goodsType);
            return goods;
        }
        return null;
    }

    @Override
    public boolean addGoods(Goods goods) {
        return goodsDao.insert(goods) > 0;
    }

    @Override
    public List<Goods> getAll() {
        List<Goods> goodsList = goodsDao.selectByCondition("", new ArrayList());
        goodsList.forEach(obj -> obj.setGoodsType(goodsTypeDao.selectById(obj.getTypeid())));
        return goodsList;
    }

    @Override
    public Page<Goods> getGoodsByPageBean(String pageNum, String pageSize, String condition, List params) {
        // 分页条件
        String newCondition = condition + " limit ?,?";
        // 查询总数
        Object count = goodsDao.selectCountsByConditon(condition, params);
        try {
            Page<Goods> pageBean = new Page<>(Integer.parseInt(pageNum), Integer.parseInt(pageSize), Integer.parseInt(count.toString()));
            params.add(pageBean.getStartIndex());
            params.add(pageBean.getPageSize());
            List<Goods> goodsList = goodsDao.selectByCondition(newCondition, params);
            goodsList.forEach(obj -> obj.setGoodsType(goodsTypeDao.selectById(obj.getTypeid())));
            pageBean.setList(goodsList);
            return pageBean;
        } catch (NumberFormatException e) {
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public boolean delete(int id) {
        return goodsDao.deleteById(id) > 0;
    }

    @Override
    public boolean update(String basePath,Goods goods) {
        Goods good = goodsDao.selectById(goods.getId());
        int a = goodsDao.updateById(goods);
        String picturePath = good.getPicture();
        // 删除原图片
        if(a > 0){
            FileUploadUtil.removeFile(basePath,picturePath);
            return true;
        }
        return false;
    }
}
